﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

using WorkStation.Frameworks.Core;
using WorkStation.Frameworks.EasyBus;
using WorkStation.Frameworks.EasyBus.Serializable;
using WorkStation.Frameworks.EasyBus.Configurations;
using WorkStation.Frameworks.EasyBus.MessageQueue;
using WorkStation.Frameworks.EasyBus.MessageQueue.RabbitMq;
using WorkStation.Frameworks.EasyBus.MessageQueue.Msmq;
using WorkStation.Frameworks.EasyBus.Pipeline;
using WorkStation.Frameworks.EasyBus.Pipeline.Handles;
using WorkStation.Frameworks.EasyBus.Handles;
using WorkStation.Frameworks.EasyBus.Extensions;
using WorkStation.Frameworks.EasyBus.Repositories;
using WorkStation.Frameworks.EasyBus.Repositories.Mongo;
using WorkStation.Frameworks.EasyBus.Repositories.ZooKeeper;

using WorkStation.Frameworks.Configuration;
using WorkStation.Frameworks.ServiceContainer;

using Microsoft.Practices.ServiceLocation;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace WorkStation.Frameworks.EasyBus.Test
{
    /// <summary>
    /// BusPublishTest 的摘要说明
    /// </summary>
    [TestClass]
    public class BusPublishTest
    {
        public BusPublishTest()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestInitialize]
        public void Init()
        {
            ConfigurationManager.GetConfiguration<ServiceContainerConfiguration>().InitServiceLocator();

            ConfigurationManager.ConfigurationChanged += configName => { };
        }

        [TestMethod]
        public void TestConfiguration() 
        {
            ServiceContainer.Current.UseServiceLocator(new RabbitMqAddressTranslator(), new MsmqAddressTranslator());

            IBus bus_Node_A = EasyBus.GetInstance("TestBus", "Server_A").SetFileConfigProvider("WorkStation.Frameworks.EasyBus.Configurations.EasyBusSettingConfiguration")
                                                                        .SetAttributeConfigProvider("WorkStation.Frameworks.EasyBus.Test")
                                                                        .SetupServer()
                                                                        .UserZooKeeper("10.32.33.140:2181", new TimeSpan(0, 0, 0, 5, 0))
                                                                        .UserMongodb("mongodb://10.32.11.131")
                                                                        .Start();
            

            bus_Node_A.Publish(new Message(new Test.PublishMessages.TestConfigPublishMessage() { Name = "ds" }, "EasyBus.TestConfigPublish"));
            
            Console.Read();
        }

        [TestMethod]
        public void TestServiceContainer() 
        {
            ServiceContainer.Current.Register<string>(() => ServiceLocator.Current.GetInstance<string>(), () => "I'm backup");
         
            var result = ServiceContainer.Current.GetInstance<string>();

            ServiceContainer.Current.RegisterCollection<IQueueAddressTranslator>(() => ServiceLocator.Current.GetAllInstances<IQueueAddressTranslator>(), () => new List<IQueueAddressTranslator>() { new RabbitMqAddressTranslator(), new MsmqAddressTranslator() });

            var collection = ServiceContainer.Current.GetAllInstances<IQueueAddressTranslator>();
        }

        [TestMethod]
        public void TestMethod1()
        {
            ServiceContainer.Current.UseServiceLocator(new RabbitMqAddressTranslator(), new MsmqAddressTranslator());

            IBus bus_Node_A = EasyBus.GetInstance("TestBus", "Server_A").SetAttributeConfigProvider("WorkStation.Frameworks.EasyBus.Test")
                                                                        .SetupServer()
                                                                        .UserZooKeeper("10.32.33.140:2181", new TimeSpan(0, 0, 0, 5, 0))
                                                                        .UserMongodb("mongodb://10.32.11.131")
                                                                        .Start();


            //IBus bus_Node_B = EasyBus.GetInstance("TestBus", "Server_B").SetAttributeConfigProvider("WorkStation.Frameworks.EasyBus.Test")
            //                                                            .SetupServer()
            //                                                            .UserZooKeeper("10.32.33.140:2181", new TimeSpan(0, 0, 0, 5, 0))
            //                                                            .UserMongodb("mongodb://10.32.11.131")
            //                                                            .Start();

            System.Threading.Thread.Sleep(3000);

            bus_Node_A.Publish(new Message(new Test.PublishMessages.TestPublishMessage() { Name = "ds" }, "EasyBus.TestPublish"));

            //Stopwatch watch = new Stopwatch();

            //watch.Start();

            //for (int i = 0; i < 100; i++)
            //{
            //    bus_Node_A.Publish(new Message(new PublishMessages.TestPublishMessage() { Name = "ds" }, "EasyBus.TestPublish") { IsRecordingHistory = true });
            //}

            //watch.Stop();

            Console.ReadLine();
        }
    }

    public class TestHandle : ProcessHandle
    {
        public TestHandle() { }

        protected override IProcessHandleResult ProcessContent(BusActionContext context)
        {
            if (!System.IO.Directory.Exists(@"D:\Message\")) 
            {
                System.IO.Directory.CreateDirectory(@"D:\Message\");
            }

            System.IO.File.WriteAllText(string.Format(@"D:\Message\{0}_{1}.txt", Guid.NewGuid().ToString(), this.Name), context.Input.ToJson());

            return new ProcessSuccessResult(context.Input, this, this.Listener.Endpoint);
        }
    }
}
